package com.share.netdisk.share.controller;

import com.share.constant.Result;
import com.share.netdisk.share.model.ShareDTO;
import com.share.netdisk.share.service.ShareService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/file")
public class ShareController {

	@Autowired
	private ShareService shareService;

	@GetMapping("/share") //生成分享链接
	public Result createShareLink(
			@RequestParam("user_id") Integer userId,
			@RequestParam("file_id") Integer fileId,
			@RequestParam(value = "expire_time", required = false, defaultValue = "7") String expireTime) {

		//调用服务层方法创建分享链接
		String shareLink = shareService.createShareLink(userId, fileId, expireTime);
		log.info("创建的分享链接为:{}", shareLink);
		//返回分享链接
		return Result.ok("分享成功",shareLink);
	}

	@PutMapping("/update")
	public Result updateShareLink(
			@RequestParam("share_id") Integer shareId,
			@RequestParam(value = "expire_time", required = false) String expireTime
			) {

		// 调用服务层方法更新分享链接
		String result = shareService.updateExpireTime(shareId, expireTime);
		log.info("Update result: {}", result);

		// 返回更新结果
		if ("更新成功".equals(result)) {
			return Result.ok(result);
		} else {
			return Result.fail(400, result, null);
		}
	}


	@PutMapping("/status") // 使用PUT请求更符合RESTful规范,逻辑删除，用户看不到但是后台可以看到
	public Result deleteShareLink(@RequestParam("share_id") Integer shareId) {
		// 调用服务层方法更新分享链接状态
		boolean result = shareService.updateStatusById(shareId, 0); // 将状态设置为0（无效）
		log.info("更新分享链接状态: {}", result);

		// 返回更新结果
		if (result) {
			return Result.ok("状态更新成功");
		} else {
			return Result.fail(500,"状态更新失败",null);
		}
	}


	//展示分享链接（个人的）
	@GetMapping("/list")
	public Result getSharesByUserId(
			@RequestParam Integer userId,
			@RequestParam(required = false) String keyword,
			@RequestParam(defaultValue = "1") int pageNumber,
			@RequestParam(defaultValue = "5") int pageSize) {
		Map<String, Object> shareData = shareService.getSharesByUserId(userId, keyword, pageNumber, pageSize);
		return Result.ok(shareData);
	}

}
